#!/usr/bin/python

'''
Created on 10 de Dez de 2011

@author: xico
'''
from Tkinter import *
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
import re
 
def newMenuBar():
    return 

def helpMenuBar():
    return 

def aboutMenuBar():
    return 
    
'''
Funcao que faz um filtro dos resultados do BeautifulSoup para contar apenas as tags certas
''' 
def filter(tag, wp, sp):
    
    Soup = sp.findAll(tag)
    
    patFinder = re.compile('<%s'%(tag))
    findPat = re.findall(patFinder , wp)
    
    numTags = len(Soup)    
    return numTags

'''
Funcao que vai escrever o resultado na Label 2
''' 
def soupAddress():
       
    webpage = urlopen(entry1.get()).read()
    
    soupWebpage = BeautifulSoup(webpage)

    numH1 = filter('h1', webpage, soupWebpage)
    numH2 = filter('h2', webpage, soupWebpage)
    numH3 = filter('h3', webpage, soupWebpage)
    numDiv = filter('div', webpage, soupWebpage)
    numP = filter('p', webpage, soupWebpage)
    numUl = filter('ul', webpage, soupWebpage)
    numOl = filter('ol', webpage, soupWebpage)
    
    label2Text.set("Numero de h1: %d \n Numero de h2: %d \n Numero de h3: %d \n Numero de div: %d \n  Numero de p: %d \n Numero de ul: %d \n Numero de ol: %d \n" %(numH1, numH2, numH3, numDiv, numP, numUl, numOl))
    
    return

'''
Criacao da aplicacao
'''
app = Tk()
app.title("Program  a do Xico")
app.geometry('450x300+200+200')

'''
Criacao da Barra de Menu
'''
menubar = Menu(app)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label='Novo', command = newMenuBar())
filemenu.add_separator()
filemenu.add_command(label='Sair', command = app.quit)

helpmenu = Menu(menubar, tearoff = 0)
helpmenu.add_command(label='Help', command = helpMenuBar())
filemenu.add_separator()
helpmenu.add_command(label='Sobre', command = aboutMenuBar())

menubar.add_cascade(label='Ficheiro', menu = filemenu)
menubar.add_cascade(label='Ajuda', menu = helpmenu)

app.config(menu=menubar)

'''
Criacao da 1a label
'''
label1Text = StringVar()
label1Text.set("Insira na caixa de Texto o Endereco...")
label1 = Label(app, textvariable = label1Text, height = 3)
label1.pack()

'''
Criacao da caixa de texto
'''
entry1 = Entry(app, width = 50)
entry1.pack() 

'''
Criacao do botao
'''
button1 = Button(app, text = "Verificar", width = 15, command = soupAddress)
button1.pack()

'''
Criacao da 2a label
'''
label2Text = StringVar()
label2Text.set("Resultado:")
label2 = Label(app, textvariable = label2Text, height = 10)
label2.pack()


app.mainloop()

